# -*- coding:utf-8 -*-
from lxml import etree
import requests
import csv
import time


def writecsv(item):
    with open("yupaowang.csv", 'a', encoding="utf-8") as f:
        writer = csv.writer(f)
        try:
            writer.writerow(item)
        except:
            print("write eroor！")


if __name__ == "__main__":

    start_url = "http://www.yupao.com/zhaogong/"
    for x in range(1, 10):
        url = start_url + str(x) + "/"
        html = requests.get(url)
        print(html)
        time.sleep(1)
        selector = etree.HTML(html.text)
        dataList = selector.xpath("/html/body/div[4]/div/div[2]/div[1]/div[1]/a/div")
        dataCount = str(len(dataList))
        print("第" + str(x) + "页" + "抓取" + dataCount + "条数据")
        for data in dataList:
            title = data.xpath("div[1]/p[1]/text()")[0]
            title.strip()
            print(title)
            info = data.xpath("div[2]/p[1]/text()")[0]
            info.strip()
            print(info)
            contacts = data.xpath("div[3]/div[1]/div[1]/div[1]/text()")[0]
            contacts.strip()
            print(contacts)
            dataTime = data.xpath("div[3]/div[1]/div[1]/p[1]/text()")[0]
            dataTime.strip()
            print(dataTime)
            item = [title, info, contacts, dataTime]
            writecsv(item)
            print("正在抓取")
